//
//  GYUserInfo.m
//  LockManagement
//
//  Created by apple on 2018/8/4.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYUserInfo.h"

static NSString *userPath = @"user.plist";


@implementation GYUserInfo

- (instancetype)initWithDic:(NSDictionary *)dic {
    if ([super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return  self;
}



- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

- (void)saveUserInfo {
    [NSKeyedArchiver archiveRootObject:self toFile:[NSString CachesDir:userPath]];
}

static GYUserInfo *_userInfo;

+ (GYUserInfo *)loadUserInfo {
    
    //    //1.首先检测这个地址文件存不存在
    BOOL isExistence = [[NSFileManager defaultManager]isExecutableFileAtPath:[NSString CachesDir:userPath]];
    
    if (_userInfo != nil && isExistence) {
        return _userInfo;
    }
    
    //解档
    _userInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString CachesDir:userPath]];
    
    return _userInfo;
    
}

+ (BOOL)removeUserInfo {
    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath:[NSString CachesDir:userPath] error:&error];
    if (error == nil) {
        NSLog(@"删除信息没错误...");
        return YES;
    }else {
        NSLog(@"删除个人信息失败....====%@",error);
        return NO;
    }
}

- (NSString *)description {
    NSArray *keys = @[@"userID",@"userPic",@"userName",@"companyID",@"companyName",@"token",@"expireTime"];
    return [NSString stringWithFormat:@"%@",[self dictionaryWithValuesForKeys:keys]];
}

#pragma mark -- 归档

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeInt:_userID forKey:@"userID"];
    [aCoder encodeObject:_userPic forKey:@"userPic"];
    [aCoder encodeObject:_userName forKey:@"userName"];
    [aCoder encodeInt:_companyID forKey:@"companyID"];
    [aCoder encodeObject:_companyName forKey:@"companyName"];
    [aCoder encodeObject:_token forKey:@"token"];
    [aCoder encodeObject:_expireTime forKey:@"expireTime"];
    
    
    
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _userID = [aDecoder decodeIntForKey:@"userID"];
        _userPic = [aDecoder decodeObjectForKey:@"userPic"];
        _userName = [aDecoder decodeObjectForKey:@"userName"];
        _companyID = [aDecoder decodeIntForKey:@"companyID"];
        _companyName = [aDecoder decodeObjectForKey:@"companyName"];
        _token = [aDecoder decodeObjectForKey:@"token"];
        _expireTime = [aDecoder decodeObjectForKey:@"expireTime"];
        
    }
    return self;
}
@end
